In [ ]:
import pandas as pd
df=pd.read_csv("../BTCUSD_Candlestick_15_M_ASK_05.08.2019-29.04.2022.csv")
In [ ]:
import plotly.io as pio
pio.renderers.default='notebook'
In [ ]:
df["Gmt time"]=df["Gmt time"].str.replace(".000","")
df['Gmt time']=pd.to_datetime(df['Gmt time'],format='%d.%m.%Y %H:%M:%S')
df.set_index("Gmt time", inplace=True)
df=df[df.High!=df.Low]
C:\Users\rhg22\AppData\Local\Temp\ipykernel_11616\729008256.py:1: FutureWarning:

The default value of regex will change from True to False in a future version.

In [ ]:
df.head()
Out[ ]:
Open High Low Close Volume
Gmt time
2019-08-05 00:00:00 11037 11104 11037 11087 8.0640
2019-08-05 00:15:00 11087 11110 11074 11099 6.9044
2019-08-05 00:30:00 11099 11172 11094 11166 7.1441
2019-08-05 00:45:00 11166 11288 11166 11273 8.3597
2019-08-05 01:00:00 11273 11308 11259 11271 10.4861
In [ ]:
import pandas_ta as ta
df["VWAP"]=ta.vwap(df.High, df.Low, df.Close, df.Volume)
In [ ]:
df.head()
Out[ ]:
Open High Low Close Volume VWAP
Gmt time
2019-08-05 00:00:00 11037 11104 11037 11087 8.0640 11076.000000
2019-08-05 00:15:00 11087 11110 11074 11099 6.9044 11084.456526
2019-08-05 00:30:00 11099 11172 11094 11166 7.1441 11103.693814
2019-08-05 00:45:00 11166 11288 11166 11273 8.3597 11141.727983
2019-08-05 01:00:00 11273 11308 11259 11271 10.4861 11176.957557
In [ ]:
df["EMA"]=ta.ema(df.Close, length=100)
In [ ]:
 
In [ ]:
emasignal = [0]*len(df)
backcandles = 6

for row in range(backcandles, len(df)):
    upt = 1
    dnt = 1
    for i in range(row-backcandles, row+1):
        if df.High[i]>=df.EMA[i]:
            dnt=0
        if df.Low[i]<=df.EMA[i]:
            upt=0
    if upt==1 and dnt==1:
        #print("!!!!! check trend loop !!!!")
        emasignal[row]=3
    elif upt==1:
        emasignal[row]=2
    elif dnt==1:
        emasignal[row]=1

df['EMASignal'] = emasignal
In [ ]:
VWAPsignal = [0]*len(df)
backcandles = 3

for row in range(backcandles, len(df)):
    upt = 1
    dnt = 1
    for i in range(row-backcandles, row+1):
        if df.High[i]>=df.VWAP[i]:
            dnt=0
        if df.Low[i]<=df.VWAP[i]:
            upt=0
    if upt==1 and dnt==1:
        #print("!!!!! check trend loop !!!!")
        VWAPsignal[row]=3
    elif upt==1:
        VWAPsignal[row]=2
    elif dnt==1:
        VWAPsignal[row]=1

df['VWAPSignal'] = VWAPsignal
In [ ]:
def TotalSignal(l):
    myclosedistance = 100
    if (df.EMASignal[l]==2 and df.VWAPSignal[l]==2#and df.EngulfingSignal[l]==2 
        and min(abs(df.VWAP[l]-df.High[l]),abs(df.VWAP[l]-df.Low[l]))<=myclosedistance):
            return 2
    if (df.EMASignal[l]==1 and df.VWAPSignal[l]==1#and df.EngulfingSignal[l]==1 
        and min(abs(df.VWAP[l]-df.High[l]),abs(df.VWAP[l]-df.Low[l]))<=myclosedistance):
            return 1
        
TotSignal = [0]*len(df)
for row in range(0, len(df)): #careful backcandles used previous cell
    TotSignal[row] = TotalSignal(row)
df['TotalSignal'] = TotSignal
In [ ]:
import numpy as np
def pointposbreak(x):
    if x['TotalSignal']==1:
        return x['High']+1e-3
    elif x['TotalSignal']==2:
        return x['Low']-1e-3
    else:
        return np.nan

df['pointposbreak'] = df.apply(lambda row: pointposbreak(row), axis=1)
In [ ]:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from datetime import datetime

dfpl = df[1500:1900]
dfpl.reset_index(inplace=True)
fig = go.Figure(data=[go.Candlestick(x=dfpl.index,
                open=dfpl['Open'],
                high=dfpl['High'],
                low=dfpl['Low'],
                close=dfpl['Close']),
                go.Scatter(x=dfpl.index, y=dfpl.EMA, line=dict(color='orange', width=1), name="EMA"),
                go.Scatter(x=dfpl.index, y=dfpl.VWAP, line=dict(color='blue', width=1), name="VWAP")])

fig.add_scatter(x=dfpl.index, y=dfpl['pointposbreak'], mode="markers",
                marker=dict(size=5, color="MediumPurple"),
                name="Signal")
fig.show()
In [ ]:
dfpl = df[:].copy()
dfpl.reset_index(inplace=True)
import pandas_ta as ta
dfpl['ATR']=ta.atr(dfpl.High, dfpl.Low, dfpl.Close, length=5)
#help(ta.atr)
def SIGNAL():
    return dfpl.TotalSignal
In [ ]:
# from backtesting import Strategy
# from backtesting import Backtest

# class MyStrat(Strategy):
#     initsize = 0.99
#     mysize = initsize
#     def init(self):
#         super().init()
#         self.signal1 = self.I(SIGNAL)

#     def next(self):
#         super().next()
#         slatr = 0.8*self.data.ATR[-1]
#         TPSLRatio = 2

#         if self.signal1==2 and len(self.trades)==0:
#             sl1 = self.data.Close[-1] - slatr
#             tp1 = self.data.Close[-1] + slatr*TPSLRatio
#             self.buy(sl=sl1, tp=tp1, size=self.mysize)
        
#         elif self.signal1==1 and len(self.trades)==0:         
#             sl1 = self.data.Close[-1] + slatr
#             tp1 = self.data.Close[-1] - slatr*TPSLRatio
#             self.sell(sl=sl1, tp=tp1, size=self.mysize)

# bt = Backtest(dfpl, MyStrat, cash=100000, margin=1/5, commission=.00)
# stat = bt.run()
# stat
In [ ]:
# bt.plot()
In [ ]: